#!/bin/bash

# 工作目录
WORK_DIR=$(pwd)

# 定义项目变量
CONFIG_DIR="sphinx"  # conf.py 配置文件目录
SOURCE_DIR="sphinx"  # 文档源文件目录
LOCALE_DIR="$CONFIG_DIR/_locales/zh_CN/LC_MESSAGES"  # 翻译文件目录

# 区分日志级别颜色
SET_COLOR_TIPS="\033[1;36m"
SET_COLOR_NORMAL=" \033[0;39m"
SET_COLOR_SUCCESS="\033[1;32m"
SET_COLOR_WARNING="\033[1;33m"
SET_COLOR_FAILURE="\033[1;31m"

# 定义日志处理函数
function LogTipMsg() {
    time=$(date "+%Y/%m/%d %T")
    printf "%b[%s] [STEP] %b%b\n" "$SET_COLOR_TIPS" "$time" "$*" "$SET_COLOR_NORMAL"
}

function LogInfoMsg() {
    time=$(date "+%Y/%m/%d %T")
    printf "[%s] [INFO] %b%b\n" "$time" "$*" "$SET_COLOR_NORMAL"
}

function LogSucMsg() {
    time=$(date "+%Y/%m/%d %T")
    printf "%b[%s] [SUC.] %b%b\n" "$SET_COLOR_SUCCESS" "$time" "$*" "$SET_COLOR_NORMAL"
}

function LogWarnMsg() {
    time=$(date "+%Y/%m/%d %T")
    printf "%b[%s] [WARN] %b%b\n" "$SET_COLOR_WARNING" "$time" "$*" "$SET_COLOR_NORMAL"
}

function LogErrorMsg() {
    time=$(date "+%Y/%m/%d %T")
    printf "%b[%s] [ERR.] %b%b\n" "$SET_COLOR_FAILURE" "$time" "$*" "$SET_COLOR_NORMAL"
}

# 检查命令是否存在
check_command() {
    if ! command -v "$1" &> /dev/null; then
        LogErrorMsg "未找到命令: $1"
        return 1
    fi
}

# 检查目录是否存在
check_directory() {
    if [ ! -d "$1" ]; then
        LogErrorMsg "目录不存在: $1"
        return 1
    fi
}

# 安装依赖
install_dependencies() {
    local deps=("sphinx-build" "sphinx-intl")
    for dep in "${deps[@]}"; do
        if ! check_command "$dep"; then
            LogWarnMsg "尝试安装缺失依赖..."
            pip install -U sphinx sphinx-intl || {
                LogErrorMsg "依赖安装失败"
                exit 1
            }
            break
        fi
    done
}

# 清理构建目录
clean_build() {
    LogTipMsg "清理旧构建文件..."
    rm -rf "$CONFIG_DIR/_build/gettext"
}

# 生成翻译模板
generate_pot_files() {
    LogTipMsg "生成翻译模板..."
    if ! sphinx-build -b gettext -c "$CONFIG_DIR" "$SOURCE_DIR" "$CONFIG_DIR/_build/gettext"; then
        LogErrorMsg "翻译模板生成失败"
        exit 1
    fi
}


# 更新翻译文件
update_translations() {
    LogTipMsg "更新翻译文件..."
    cd "$CONFIG_DIR" || exit 1

    if ! sphinx-intl update -p _build/gettext -l zh_CN; then
        LogErrorMsg "翻译更新失败"
        exit 1
    fi

    if ! sphinx-intl build --locale-dir _locales; then
        LogErrorMsg "翻译编译失败"
        exit 1
    fi
}

show_results() {
    cd "$WORK_DIR" || exit 1
    LogTipMsg "检查翻译文件..."

    # 使用ls匹配
    shopt -s nullglob  # 无匹配时返回空
    po_files=("$LOCALE_DIR"/*.po)
    shopt -u nullglob

    if [ ${#po_files[@]} -eq 0 ]; then
        LogWarnMsg "未找到任何 .po 文件"
        # 尝试备用方法
        if ! ls "$LOCALE_DIR"/*.po &>/dev/null; then
            LogErrorMsg "确认无翻译文件生成"
            return 1
        fi
    else
        LogSucMsg "找到.po文件:"
        for file in "${po_files[@]}"; do
            echo "  ${COLOR_SUCCESS}✔${COLOR_NORMAL} ${file##*/}"
        done
    fi

    # 详细列表
    echo -e "\n文件详细信息:"
    ls -l "$LOCALE_DIR"/*.po 2>/dev/null || echo "  (无.po文件)"
}

main() {
    # 前置检查
    install_dependencies
    check_directory "$CONFIG_DIR"
    check_directory "$SOURCE_DIR"

    # 构建流程
    clean_build
    generate_pot_files
    update_translations

    # 结果展示
    show_results
}

# 执行主函数
main "$@"